How to store a string and display only part of it

时间:2015-06-25 19:12:06

标签: javascript php mysql

I am using datatables with mysql and php to display records and I have a column called 'Notes' which may contain 1 or more newlines, ( chr(10) ). I am using while( $row=mysqli_fetch_assoc($records) ){...... to fill the table and $('#example tbody').on( 'click', 'tr', function ()...... and Notes = $('td', this).eq(6).text(); to get the value of 'notes' (and others) in the clicked row for populating an edit dialog. All working fine. I have been asked if it's possible to display only the first line of the notes column in the table which I have done using 'strpos' and 'substr' and it's OK Unfortunately because the edit dialog gets it's data from the table I'm now only getting the first line..... I'm guessing I need to collect the entire field before I chop off the back bit and then use this collection (maybe in an array ??) to populate the edit dialog. I'm afraid I'm still a bit 'green' here and not sure of the best approach or how to fill and then access the array. Thanks for any help. Additional ....Additional ....Additional .... Thanks Barmar, this looks like a nice clean fix but I'm not there yet..... I have added the CSS and the following code snippets.. ** ListNotes = $('td:nth-child(6) .expanded', this).text(); and inside the php table 'while loop' $findme = chr(10); $pos = strpos($row['ListNotes'], $findme); $lft = substr($row['ListNotes'], 0, $pos); .... echo "<td><span class='abbreviated'>".$lft."</span><span class='expanded'>".$row['ListNotes']."</span></td>"; and the table is displayed as expected. I put a break in Firebug after the line marked ** and 'ListNotes' is an empty string ("") ??? I was hoping it would contain the whole string including any chr(10)'s. Have I done something wrong?

2 个答案:

答案 0 :(得分:0)

将全文放在隐藏的范围内:

<td><span class="abbreviated">First line...</span><span class="expanded">First line<br>Second line<br>Third line</span></td>

使用CSS:

.expanded {
  display: none;
}

编辑时,请从.expanded范围

中获取要扩展的注释
var text = $('td:nth-child(6) .expanded', this).text();

编辑后,将缩写和展开的结果存储回适当的跨度:

$('td:nth-child(6) .expanded', this).text(edited_text);
$('td:nth-child(6) .abbreviated', this).text(abbreviated_text);

答案 1 :(得分:0)

我的意思是不久前回答我自己的问题但是忘记了。这是我解决它的方式。 在表格显示中,我有1个显示的列,其中包含截断的字符串,1个隐藏的列包含完整的字符串。我使用隐藏列进行编辑对话框。简单有效。