yii2 html 获取字段id,如何在yii2中使用javascript获取列值而不是ID(How to get column value rather than ID in yii2 by usi...

如何在yii2中使用javascript获取列值而不是ID(How to get column value rather than ID in yii2 by using javascript)

我想要显示表格中列的值。 我有一个Gridview ,我在其中放置了一个checkbox column 。 在选中复选框并单击以显示值时,我放置了一个javascript ,其中我使用了一个循环来收集所选复选框的信息。 但点击按钮,我只能看到checkbox ID 。 以下是我所做的

行动

$sql = "SELECT DISTINCT ms.`meter_msn` AS 'Meter Serial Number', u.`name` AS 'Issued To',ps.`name` AS 'Store' FROM `meters` ms

INNER JOIN `ogp_header` ogph ON ms.`issued_user` = ogph.`issuer`

INNER JOIN `project_store` ps ON ogph.`store_id` = ps.`id`

INNER JOIN `user` u ON `ogph`.`issuer` = u.`id`

$where AND ms.`meter_status` = 'Installation Ready' AND ogph.`status` IN ('Prepared', 'Canceled')

ORDER BY ms.`id` DESC "

$dataProvider = new SqlDataProvider([

'sql' => $sql, // $sql is the resulted query

'totalCount' => $count,

'pagination' => [

'pageSize' => 30,

],

]);

return $this->render('viewcreated', [

'dataProvider' => $dataProvider,

'model' => $this->findModel($id),

'id'=> $model->id

/*'searchModel' => $searchModel*/

]);

我的看法

= GridView::widget([

'dataProvider' => $dataProvider,

/*'filterModel' => $searchModel,*/

'id'=>'grid',

'columns' => [

['class' => 'yii\grid\CheckboxColumn'],

'Meter Serial Number',

'Issued To',

'Store',

],

View Selected Rows

在我的javascript我已经完成

$(document).ready(function () {

$('#myid').click(function() {

var strValue = "";

$('input[name="selection[]"]:checked').each(function() {

if(strValue!="")

{

strValue = strValue + " , " + this.value;

}

else

strValue = this.value;

});

alert(strValue);

}) });

现在当我点击按钮时我只看到ID ,但我想显示Meter Serial Number 。

5bb338a5d05850f6ef925b401f186f9a.png

我已经找到它,但无法找到解决方案

任何帮助将不胜感激。

I want to display the value(s) of the column which is in my table. I have a Gridview in which I have placed a checkbox column. On selecting the check box and clicking on the to show the value(s) I have placed a javascript in which I have used a loop that will collect the information of the selected check box. But on click on the button I am able to see only the checkbox ID. Below is what I have done

Action

$sql = "SELECT DISTINCT ms.`meter_msn` AS 'Meter Serial Number', u.`name` AS 'Issued To',ps.`name` AS 'Store' FROM `meters` ms

INNER JOIN `ogp_header` ogph ON ms.`issued_user` = ogph.`issuer`

INNER JOIN `project_store` ps ON ogph.`store_id` = ps.`id`

INNER JOIN `user` u ON `ogph`.`issuer` = u.`id`

$where AND ms.`meter_status` = 'Installation Ready' AND ogph.`status` IN ('Prepared', 'Canceled')

ORDER BY ms.`id` DESC "

$dataProvider = new SqlDataProvider([

'sql' => $sql, // $sql is the resulted query

'totalCount' => $count,

'pagination' => [

'pageSize' => 30,

],

]);

return $this->render('viewcreated', [

'dataProvider' => $dataProvider,

'model' => $this->findModel($id),

'id'=> $model->id

/*'searchModel' => $searchModel*/

]);

My view

= GridView::widget([

'dataProvider' => $dataProvider,

/*'filterModel' => $searchModel,*/

'id'=>'grid',

'columns' => [

['class' => 'yii\grid\CheckboxColumn'],

'Meter Serial Number',

'Issued To',

'Store',

],

View Selected Rows

In my javascript i have done

$(document).ready(function () {

$('#myid').click(function() {

var strValue = "";

$('input[name="selection[]"]:checked').each(function() {

if(strValue!="")

{

strValue = strValue + " , " + this.value;

}

else

strValue = this.value;

});

alert(strValue);

}) });

Now when i click on the button I only see the ID, but I want to display the value(s) of Meter Serial Number.

5bb338a5d05850f6ef925b401f186f9a.png

I have searched for it but unable to find a solution

Any help would be highly appreciated.

原文:https://stackoverflow.com/questions/47227369

更新时间:2020-02-04 19:39

最满意答案

如果你能给出这些元素的html代码,那么给你工作代码会更好。

您可以通过定位可能与Id相关的此元素的某些标识(对每个元素唯一)来获取此值。 你必须改变 - > this.value < - 通过其他代码。

if(strValue!="")

{

strValue = strValue + " , " + this.value;

}

else

strValue = this.value;

If you can give html code of this elements this would be better for give you working code.

you can get this value by targeting some identity of this element which may be related to Id( which is unique to each element). And you have to change -> this.value

if(strValue!="")

{

strValue = strValue + " , " + this.value;

}

else

strValue = this.value;

相关问答

最后我找到了解决办法。 它取决于Yii2扩展和AssetBundles 。 这个故事很简单,只需将一个文件夹中的所有文件放置在默认的Yii2文件夹中,例如:common,vendor.- 顺便提一下,供应商可以在基本和高级yii2应用程序的模板中找到 - 。 除了所有的文件,即我的情况下 ,小部件类php文件和JavaScript文件,你必须创建YourWidgetNameAsset php类文件。 事实上,解决方案的主要关键在于该类。 我的情况 我有一个名为JuiThemeSelectWidge

...

是的,你可以,只需在模型类中添加一个公共属性: class MyClass extends \yii\db\ActiveRecord

{

public $myAttribute;

...

您还需要将其声明为安全属性,并在规则方法中输入: public function rules()

{

...

[['myAttribute'], 'safe'],

...

之后,您可以使用其他规则进行任何处理: public function rules()

{

...

使用$key->relationName->fieldName : foreach ($dosis as $key) {

$items[$key->id_dosis] = [

'content' => $key->relationName->fieldName,

'options' => ['data' => ['id_dosis'=>$key->relationName->fieldName]],

];

}

Use $key->relationN

...

亲爱的,你忘了registerJs() 。 并且你的js语法是错误的。请使用它。 <?php

$script = <<

$(window).load(function(){

$.get(

'index.php?r=salary/ratechart/get-for-ratechart',

{ rateid : 1 },

function(data){

alert(data);

var data = $.parseJSON(data);

}

...

您创建的迁移已标记为完成(在数据库中检查表“迁移”)。 如果要向该表添加新列,则必须停止迁移并再次运行它或创建新的迁移。 The migration you created had been marked as done (check table 'migration' in your database). If you want to add new column to the that table you have to down the migration and run it again

...

经过太多的反复试验后,我找到了自己问题的解决方案。 查看:_form.php <?php

use yii\helpers\Html;

use yii\widgets\ActiveForm;

use kartik\widgets\FileInput;

/**

* @var yii\web\View $this

* @var backend\models\Product $model

* @var yii\widgets\ActiveForm $form

*/

?>

...

您需要在WHERE子句中使用正确的表别名为nombre字段添加前缀。 假设您正在根据responsables的nombre字段进行过滤,那么您的查询应该是: SELECT COUNT(*)

FROM `agencias`

LEFT JOIN `responsables` `rsp`

ON `agencias`.`responsable` = `rsp`.`responsable_id`

LEFT JOIN `receptores` `rec`

...

如果你能给出这些元素的html代码,那么给你工作代码会更好。 您可以通过定位可能与Id相关的此元素的某些标识(对每个元素唯一)来获取此值。 你必须改变 - > this.value < - 通过其他代码。 if(strValue!="")

{

strValue = strValue + " , " + this.value;

}

else

strValue = this.value;

If you can give html code of this ele

...

你可以试试 Yii::$app->session->getId();

本指南对您有用http://www.yiiframework.com/doc-2.0/guide-runtime-sessions-cookies.html 尝试检查会话之前是否已处于活动状态,如果不使用会话open()。 session = Yii::$app->session;

// check if a session is already open

if ($session->isActive) ...

// op

...

您可以使用以下方法之一进行一些修改: //pk => programa

$data = [

1 => 'programa1',

2 => 'programa2',

3 => 'programa3',

];

1)使用JS = $form->field(new \app\models\Tours(), 'nombre')->dropDownList($data, ['prompt' => 'Programa'])->label(false); ?>

JS $this->regist

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值